import sys
import queryParser
import index

class SearchEngine :
	def __init__(self, index, queryParser=None) :
	    if queryParser == None :
	        self.queryParser = queryParser.QueryParser()
	    else :
	        self.queryParser = queryParser
	    self.index = index
	
	# TODO: real results!    
	def get_results(self, query) :
	    return ['result1', 'result2', 'result3']
	
if __name__ == "__main__" :

    	
    if len(sys.argv) < 3 :
        print "Too few arguments! Usage: python main.py <queries> <results> | python main.py makeIndex <documents>"
    elif len(sys.argv) == 3 and sys.argv[1] == 'makeIndex':
        index = index.Index()
        index.parse(sys.argv[2])
        index.save()       
    else:
        queries_filename = sys.argv[1]
        results_filename = sys.argv[2]
        
        qParser = queryParser.QueryParser()
        index = index.Index()
        engine = SearchEngine(queryParser=qParser, index=index)
        
        queries_file = open(queries_filename, 'r')
        results_file = open(results_filename, 'w')
        for query in queries_file :
            query = query.strip()
            results = engine.get_results(query)
            results_file.write("QUERY: "+query+" TOTAL: "+str(len(results))+"\n")
            for result in results :
                results_file.write(result + "\n")
        queries_file.close()
        results_file.close()
        
